package com.yunc.modules.chat.controller;

import com.yunc.modules.chat.entity.ChatFriendsGroups;
import com.yunc.modules.chat.entity.vo.ChatFriendsGroupsVO;
import com.yunc.modules.chat.entity.vo.ChatFriendsVO;
import com.yunc.modules.chat.service.IChatFriendsGroupsService;
import com.yunc.modules.sys.entity.SysUser;
import com.yunc.modules.sys.service.ISysUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunc.base.controller.CommonController;
import org.apache.commons.lang3.StringUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.yunc.common.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.modules.chat.entity.ChatFriends;
import com.yunc.modules.chat.service.IChatFriendsService;

import java.util.ArrayList;
import java.util.List;

/**
 * 好友表 前端控制器
 *
 * @author concong tao
 * @since 2019-03-28
 */
@RestController
@RequestMapping("/chat/friends")
public class ChatFriendsController extends CommonController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IChatFriendsService chatFriendsService;
    @Autowired
    private IChatFriendsGroupsService chatFriendsGroupsService;

    @GetMapping("/page")
    public R page(Page<ChatFriends> page, ChatFriends chatFriends){
        chatFriendsService.page(page, new QueryWrapper<>(chatFriends));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(ChatFriends chatFriends){
        // 先查询组
        ChatFriendsGroups chatFriendsGroups = new ChatFriendsGroups();
        chatFriendsGroups.setUserId(getUserId());
        List<ChatFriendsGroups> list = chatFriendsGroupsService.list(new QueryWrapper<>(chatFriendsGroups));
        List<ChatFriendsGroupsVO> groupList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (ChatFriendsGroups friendsGroup : list) {
                ChatFriendsGroupsVO chatFriendsGroup = new ChatFriendsGroupsVO();
                chatFriendsGroup.setId(friendsGroup.getId());
                // chatFriendsGroup.setOnline(1);
                chatFriendsGroup.setGroupname(friendsGroup.getName());
                // 查询朋友
                List<ChatFriends> chatFriendses = chatFriendsService.list(new QueryWrapper<ChatFriends>()
                        .eq("friend_group_id", friendsGroup.getId()));
                List<ChatFriendsVO> chatFriendList = new ArrayList<>();
                if (chatFriendses != null && chatFriendses.size() > 0) {
                    for (ChatFriends friend : chatFriendses) {
                        SysUser sysUser = sysUserService.getById(friend.getFriendId());
                        ChatFriendsVO chatFriend = new ChatFriendsVO();
                        chatFriend.setId(sysUser.getId());
                        chatFriend.setUsername(sysUser.getUserName());
                        chatFriend.setAvatar(sysUser.getHeadLogo());
                        // 状态
                        if (StringUtils.equals(sysUser.getLineStatus(), "2")) {
                            chatFriend.setStatus("online");
                        } else {
                            chatFriend.setStatus("offline");
                        }
                        chatFriend.setSign(sysUser.getRemark());
                        chatFriendList.add(chatFriend);
                    }
                }
                chatFriendsGroup.setList(chatFriendList);
                groupList.add(chatFriendsGroup);
            }
        }
        return R.ok().put("data", groupList);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable Long id){
        ChatFriends chatFriends = chatFriendsService.getById(id);
        return R.ok().put("chatFriends", chatFriends);
    }

    @PostMapping("/save")
    public R save(@RequestBody ChatFriends chatFriends){
        return chatFriendsService.saveFriends(chatFriends);
    }

    @PutMapping(value = "/update")
    public R update(@RequestBody ChatFriends chatFriends){
        boolean flag = chatFriendsService.updateById(chatFriends);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable String ids){
        if (StringUtils.isEmpty(ids)) {
            return R.error("传入值为空");
        }
        List<String> idsList = StrSpliter.split(ids, ',', 0, true, true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = chatFriendsService.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
